package com.bocai.freeman.view.activitys;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bocai.freeman.R;
import com.bocai.freeman.base.BaseActivity;
import com.bocai.freeman.utils.ToolbarHelper;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by admin on 2016/5/19.
 * 提现
 */
public class WithdrawActivity extends BaseActivity {

    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(R.id.tv_balance)
    TextView mTvBalance;
    @Bind(R.id.ll_pay_style)
    LinearLayout mLlPayStyle;
    @Bind(R.id.tv_alipay)
    TextView mTvAlipay;

    private AlertDialog mDialog;
    private View dialogView;
    private RelativeLayout mRlAlipay, mRlWepay;
    private TextView mCancel, mSure;
    private ImageView mIvAlipay, mIvWepay;

    private int POSITION = -1;//0为支付宝，1为微信,-1为初始状态

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    @Override
    protected void initView() {
        super.initView();
        dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_withdraw_style, null);
        mDialog = new AlertDialog.Builder(this).setView(dialogView).create();
        mRlAlipay = (RelativeLayout) dialogView.findViewById(R.id.rl_alipay);
        mRlWepay = (RelativeLayout) dialogView.findViewById(R.id.rl_wexin_pay);
        mCancel = (TextView) dialogView.findViewById(R.id.tv_return);
        mSure = (TextView) dialogView.findViewById(R.id.tv_sure);
        mIvAlipay = (ImageView) dialogView.findViewById(R.id.iv_alipay);
        mIvWepay = (ImageView) dialogView.findViewById(R.id.iv_wepay);

        mRlAlipay.setOnClickListener(v -> {
            if (POSITION == -1 || POSITION == 1) {
                if (POSITION == 1) mIvWepay.setImageResource(R.mipmap.uncheck);
                mIvAlipay.setImageResource(R.mipmap.checked);
                POSITION = 0;
            }
        });

        mRlWepay.setOnClickListener(v -> {
            if (POSITION == -1 || POSITION == 0) {
                if (POSITION == 0) mIvAlipay.setImageResource(R.mipmap.uncheck);
                mIvWepay.setImageResource(R.mipmap.checked);
                POSITION = 1;
            }
        });

        mCancel.setOnClickListener(v -> mDialog.dismiss());
        mSure.setOnClickListener(v -> {
            if (POSITION == 0)mTvAlipay.setText(getString(R.string.alipay));
            else mTvAlipay.setText(getString(R.string.wechat));
            mDialog.dismiss();
        });
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ToolbarHelper.setup(this, R.string.withdraw, R.mipmap.left_return);
        mTvBalance.setText(String.format(getString(R.string.current_balance), "5000"));
        click(mLlPayStyle).subscribe(o -> {
            if (mDialog.isShowing()) mDialog.hide();
            else mDialog.show();
        });
    }

    @Override
    public void setData(Object obj) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
